package org.example.script.utils;

import org.example.model.*;
import org.example.utils.CommonUtils;
import org.example.utils.GlobalUtils;
import org.openqa.selenium.WebDriver;

public class AdvertisementExecutor {

    private WebDriver webDriver;

    private Device device;

    private String [] adEnterPageFlag;

    private String [] adEndFlag;

    private String [] continueAdVideoFlag;

    private String [] endAdVideoFlag;

    private String saveFilePath;

    private CallBackAndReturn isTaskPage;

    private CallBack customExecution;

    public AdvertisementExecutor() {}

    public void initParams(WebDriver webDriver, Device device,
                           String [] adEnterPageFlag, String [] adEndFlag,
                           String [] continueAdVideoFlag, String [] endAdVideoFlag,
                           String saveFilePath, CallBackAndReturn isTaskPage, CallBack customExecution) {
        this.webDriver = webDriver;
        this.device = device;

        this.adEnterPageFlag = adEnterPageFlag;
        this.adEndFlag = adEndFlag;
        this.continueAdVideoFlag = continueAdVideoFlag;
        this.endAdVideoFlag = endAdVideoFlag;
        this.isTaskPage = isTaskPage;
        this.customExecution = customExecution;
        this.saveFilePath = saveFilePath;
    }

    public boolean execute() {
        Widget widget;
        for (int i = 0; i < 3; i++) {
            widget = CommonUtils.findMoreWidgetByOcr(webDriver, adEnterPageFlag, saveFilePath);
            if (widget != null) {
                GlobalUtils.logi("已进入广告页面");
                break;
            } else {
                GlobalUtils.logi("未发现广告页面，等待2s");
                GlobalUtils.sleep(2000);
            }
        }
        widget = CommonUtils.findMoreWidgetByOcr(webDriver, adEnterPageFlag, saveFilePath);
        if (widget == null) {
            GlobalUtils.logi("未进入广告页，广告任务结束！");
            return false;
        }

        GlobalUtils.logi("已进入广告页，等待广告播放完毕！");
        for (int i = 0; i < 5; i++) {
            widget = CommonUtils.findMoreWidgetByOcr(webDriver, adEndFlag, saveFilePath);
            if (widget != null) {
                GlobalUtils.logi("广告播放完毕");
                break;
            } else {
                GlobalUtils.logi("暂未播放完毕，等待5s");
                GlobalUtils.sleep(5000);
            }
        }
        for (int i = 0; i < 5; i++) {
            GlobalUtils.back(webDriver);
            GlobalUtils.sleep(3000);
            Widget continueWidget = CommonUtils.findMoreWidgetByOcr(webDriver, continueAdVideoFlag, saveFilePath);
            if (continueWidget != null) {
                GlobalUtils.logi("点击继续观看");
                CommonUtils.myClick(webDriver, device, continueWidget, 2000L);
                GlobalUtils.sleep(2000);
                execute();
            }

            Widget endWidget = CommonUtils.findMoreWidgetByOcr(webDriver, endAdVideoFlag, saveFilePath);
            if (endWidget != null) {
                GlobalUtils.logi("点击退出");
                CommonUtils.myClick(webDriver, device, endWidget, 2000L);
                GlobalUtils.sleep(2000);
            }
            if (isTaskPage.execute()) {
                GlobalUtils.logi("已退出到任务页，继续执行任务！");
                return true;
            }
        }
        return false;
    }
}
